package net.csdn.business.common.oauth2.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import net.csdn.business.common.oauth2.mapper.OauthRefreshTokenMapper;
import net.csdn.business.common.domain.request.oauth.NewOauthRefreshTokenDTO;
import net.csdn.business.common.domain.request.oauth.UpdateOauthRefreshTokenDTO;
import net.csdn.business.common.oauth2.model.entity.OauthRefreshToken;
import net.csdn.business.common.domain.request.oauth.OauthRefreshTokenQuery;
import net.csdn.business.common.domain.vo.oauth.OauthRefreshTokenVO;
import net.csdn.business.common.oauth2.service.IOauthRefreshTokenService;
import net.csdn.business.common.service.SnowflakeService;
import net.csdn.business.common.service.SuperServiceImpl;
import net.csdn.business.common.utils.UserInfoParser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**
 * refreshToken信息Service业务层处理
 * 
 * @author zhangyl
 * @date 2022-11-08
 */
@Service
public class OauthRefreshTokenServiceImpl extends SuperServiceImpl<OauthRefreshTokenMapper, OauthRefreshToken> implements IOauthRefreshTokenService
{
    @Autowired
    private SnowflakeService snowflakeService;

    /**
     * 根据id查询refreshToken信息
     * 
     * @param tokenId refreshToken信息主键
     * @return refreshToken信息
     */
    @Override
    public OauthRefreshTokenVO selectByTokenId(String tokenId)
    {
        OauthRefreshTokenVO result=null;
        LambdaQueryWrapper<OauthRefreshToken> queryWrapper = new QueryWrapper<OauthRefreshToken>().lambda();
        queryWrapper.eq(OauthRefreshToken::getTokenId, tokenId);
        OauthRefreshToken oauthRefreshToken=baseMapper.selectOne(queryWrapper);
        if(oauthRefreshToken!=null){
            result=new OauthRefreshTokenVO();
            BeanUtils.copyProperties(oauthRefreshToken, result);
        }
        return result;
    }

    /**
     * 查询refreshToken信息列表
     * 
     * @param query refreshToken信息
     * @return refreshToken信息
     */
    @Override
    public List<OauthRefreshTokenVO> selectOauthRefreshTokenList(OauthRefreshTokenQuery query)
    {
        return baseMapper.selectOauthRefreshTokenList(query);
    }

    /**
     * 新增refreshToken信息
     *
     * @param newOauthRefreshTokenDTO refreshToken信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer insertOauthRefreshToken(NewOauthRefreshTokenDTO newOauthRefreshTokenDTO)
    {
        OauthRefreshToken oauthRefreshToken=new OauthRefreshToken();
        BeanUtils.copyProperties(newOauthRefreshTokenDTO,oauthRefreshToken);
        oauthRefreshToken.setObjectId(snowflakeService.nextNo());
        oauthRefreshToken.setCreatedDate(new Date());
        return baseMapper.insert(oauthRefreshToken);
    }

    /**
     * 修改refreshToken信息
     *
     * @param updateOauthRefreshTokenDTO refreshToken信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateOauthRefreshToken(UpdateOauthRefreshTokenDTO updateOauthRefreshTokenDTO)
    {
        String id=updateOauthRefreshTokenDTO.getObjectId();
        LambdaUpdateWrapper<OauthRefreshToken> updateWrapper = new UpdateWrapper<OauthRefreshToken>().lambda();
        updateWrapper.eq(OauthRefreshToken::getObjectId, id);
        OauthRefreshToken oauthRefreshToken=new OauthRefreshToken();
        BeanUtils.copyProperties(updateOauthRefreshTokenDTO,oauthRefreshToken);
        oauthRefreshToken.setLastModifiedBy(UserInfoParser.getUserId());
        oauthRefreshToken.setLastModifiedDate(new Date());
        return baseMapper.update(oauthRefreshToken,updateWrapper);
    }

    /**
     * 删除refreshToken信息信息
     * 
     * @param id refreshToken信息主键
     * @return 结果
     */
    @Override
    public Integer deleteOauthRefreshTokenById(String id)
    {
        LambdaQueryWrapper<OauthRefreshToken> queryWrapper = new QueryWrapper<OauthRefreshToken>().lambda();
        queryWrapper.eq(OauthRefreshToken::getObjectId, id);
        return baseMapper.delete(queryWrapper);
    }

    /**
     * 删除refreshToken信息信息
     *
     * @param tokenId refreshToken信息主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteByTokenId(String tokenId)
    {
        LambdaQueryWrapper<OauthRefreshToken> queryWrapper = new QueryWrapper<OauthRefreshToken>().lambda();
        queryWrapper.eq(OauthRefreshToken::getTokenId, tokenId);
        return baseMapper.delete(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteRefreshToken(OauthRefreshTokenQuery query) {
        LambdaQueryWrapper<OauthRefreshToken> queryWrapper = new QueryWrapper<OauthRefreshToken>().lambda();
        String clientId=query.getClientId();
        String userName=query.getUserName();
        if(StringUtils.isBlank(clientId)&&StringUtils.isBlank(userName)){
            return 0;
        }
        if(StringUtils.isNotBlank(clientId)){
            queryWrapper.eq(OauthRefreshToken::getClientId, clientId);
        }
        if(StringUtils.isNotBlank(userName)){
            queryWrapper.eq(OauthRefreshToken::getUserName,userName);
        }
        return baseMapper.delete(queryWrapper);
    }
}
